{% extends 'base.html' %}
{% load static %}

{% block title %}设备管理 - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>设备管理</h1>
        <a href="{% url 'assets:asset_create' %}" class="btn btn-primary">
            <i class="bi bi-plus-circle"></i> 添加设备
        </a>
    </div>

    {% if messages %}
    <div class="alert alert-info">
        {% for message in messages %}
        <div>{{ message }}</div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- 搜索和筛选 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control" placeholder="搜索设备名称、编号、型号或用户" value="{{ search_query }}">
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-select">
                        <option value="">所有状态</option>
                        {% for value, label in status_choices %}
                        <option value="{{ value }}" {% if status_filter == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="category" class="form-select">
                        <option value="">所有分类</option>
                        {% for value, label in category_choices %}
                        <option value="{{ value }}" {% if category_filter == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-outline-primary w-100">
                        <i class="bi bi-search"></i> 搜索
                    </button>
                </div>
                <div class="col-md-2">
                    <a href="{% url 'assets:asset_manage' %}" class="btn btn-outline-secondary w-100">
                        <i class="bi bi-arrow-clockwise"></i> 重置
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- 设备列表 -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">设备列表</h5>
        </div>
        <div class="card-body">
            {% if assets %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>设备编号</th>
                            <th>设备名称</th>
                            <th>型号</th>
                            <th>分类</th>
                            <th>状态</th>
                            <th>分配用户</th>
                            <th>购入日期</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for asset in assets %}
                        <tr>
                            <td>
                                <strong>{{ asset.code|default:"未设置" }}</strong>
                            </td>
                            <td>{{ asset.name }}</td>
                            <td>{{ asset.model|default:"-" }}</td>
                            <td>
                                <span class="badge bg-secondary">{{ asset.get_category_display }}</span>
                            </td>
                            <td>
                                <span class="badge bg-{% if asset.status == 'active' %}success{% elif asset.status == 'maintenance' %}warning{% elif asset.status == 'retired' %}danger{% else %}secondary{% endif %}">
                                    {{ asset.get_status_display }}
                                </span>
                            </td>
                            <td>
                                {% if asset.assigned_to %}
                                {{ asset.assigned_to.get_full_name|default:asset.assigned_to.username }}
                                {% else %}
                                <span class="text-muted">未分配</span>
                                {% endif %}
                            </td>
                            <td>{{ asset.purchase_date|date:"Y-m-d"|default:"-" }}</td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'assets:asset_admin_detail' asset.pk %}" class="btn btn-outline-primary">
                                        <i class="bi bi-eye"></i> 查看
                                    </a>
                                    <a href="{% url 'assets:asset_admin_edit' asset.pk %}" class="btn btn-outline-info">
                                        <i class="bi bi-pencil"></i> 编辑
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if assets.has_other_pages %}
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if assets.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ assets.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}">上一页</a>
                    </li>
                    {% endif %}

                    {% for num in assets.paginator.page_range %}
                    <li class="page-item {% if assets.number == num %}active{% endif %}">
                        <a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}">{{ num }}</a>
                    </li>
                    {% endfor %}

                    {% if assets.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ assets.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}">下一页</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}

            {% else %}
            <div class="text-center py-5">
                <div class="text-muted">
                    <i class="bi bi-pc-display" style="font-size: 3rem;"></i>
                    <h4 class="mt-3">暂无设备</h4>
                    <p>系统中还没有设备数据</p>
                    <a href="{% url 'assets:asset_create' %}" class="btn btn-primary mt-2">
                        <i class="bi bi-plus-circle"></i> 添加第一个设备
                    </a>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<style>
.table th {
    background-color: #f8f9fa;
    border-top: none;
}
.btn-group .btn {
    border-radius: 0.25rem;
}
.btn-group .btn:not(:last-child) {
    margin-right: 0.25rem;
}
</style>
{% endblock %}
